package club.dataresources.service.impl;

import club.dataresources.bo.BarberWorkTimeBo;
import club.dataresources.entity.Barber;
import club.dataresources.entity.BarberExample;
import club.dataresources.mapper.BarberMapper;
import club.dataresources.service.BarberService;
import club.dataresources.service.BarberWorkTimeService;
import club.dataresources.util.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @author
 * @create 2018-11-11 10:17
 **/
@Service
public class BarberServiceImpl implements BarberService{
    @Autowired
    private BarberMapper barberMapper;

    @Autowired
    private BarberWorkTimeService barberWorkTimeService;

    @Override
    public boolean addBarber(Barber barber) throws Exception {
        return barberMapper.insertSelective(barber)==1;
    }

    @Override
    public boolean removeBarber(Integer barberId) throws Exception {
        return barberMapper.deleteByPrimaryKey(barberId)==1;
    }

    @Override
    public boolean updateBarber(Barber barber) throws Exception {
        return barberMapper.updateByPrimaryKeySelective(barber)==1;
    }

    @Override
    public Page<Barber> listBarber(Integer page, Integer rows, Barber barber) throws Exception {
        PageHelper.startPage(page, rows);
        BarberExample example = new BarberExample();
        BarberExample.Criteria criteria = example.createCriteria();

        //如果是模糊查询
        if(barber.getBarberLevel()!=null){
            criteria.andBarberLevelEqualTo(barber.getBarberLevel());
        }
        if(barber.getBarberName()!=null){
            criteria.andBarberNameLike(barber.getBarberName());
        }
        criteria.andBarberIdIsNotNull();
        return barberMapper.selectByExample(example);
    }

    /**
     * 获取一个理发师(按时间最近有空，同样有空时按档次较低的规则);
     * @return  Barber
     * @throws Exception
     */
    @Override
        public Barber getBarber() throws Exception {
        List<BarberWorkTimeBo> liBwt = barberWorkTimeService.getAllBarberWorkerTime(0,100);
        Integer[] bwtArrays = new Integer[7];
        //获得用数组表示的理发师时间表 分别是星期一到星期天 1为在班，0为不在班
        //和用字符串表示的理发师时间表 分别是星期一到星期天 1为在班，0为不在班
        for(BarberWorkTimeBo bwt:liBwt){
            bwtArrays[0] = bwt.getMonday();
            bwtArrays[1] = bwt.getTuesday();
            bwtArrays[2] = bwt.getWednesday();
            bwtArrays[3] = bwt.getThursday();
            bwtArrays[4] = bwt.getFriday();
            bwtArrays[5] = bwt.getSaturday();
            bwtArrays[6] = bwt.getSunday();
            bwt.setLiBwt(bwtArrays);
            String weekStr = "";
            for(int i=0;i<7;i++){
                weekStr = weekStr + bwtArrays[i];
            }
            bwt.setLiWeek(weekStr);
        }
        //先截取字符串，再indexOf，记录距离最近的值 获得一个数组
        //并且拷贝一个数组 方便后面使用
        Integer[] minArrays = new Integer[liBwt.size()];
        Integer[] copyArrays = new Integer[liBwt.size()];
        for(int i=0;i<liBwt.size();i++){
            minArrays[i] = liBwt.get(i).getLiWeek().substring(DateUtil.getWeekDayTodayInt()).indexOf("1");
            copyArrays[i] = minArrays[i];
        }

        //找出这个数组中的最小值，将所有与最小值相同的索引获得对应对象加入到一个List中得到理发师初步筛选结果
        Arrays.sort(copyArrays);
        int min = copyArrays[0];
        List<Barber> liMinBarber = new ArrayList<Barber>();
        for(int i=0;i<minArrays.length;i++){
            if(minArrays[i]==min){
                Barber barber = barberMapper.selectByPrimaryKey(liBwt.get(i).getBarberId());
                liMinBarber.add(barber);
            }
        }

        //判断获得List的size 如果大于0找出档次较低的barber
        if(liMinBarber.size()==1){
            return liMinBarber.get(0);
        }else{
            for(Barber b:liMinBarber){
                if(b.getBarberLevel().equals("学生档")){
                    return b;
                }
            }
            for(Barber b:liMinBarber){
                if(b.getBarberLevel().equals("中高端")){
                    return b;
                }
            }
            //如果只有VIP
            Random rand = new Random();
            int VIP = rand.nextInt((liMinBarber.size() - 0) + 1) + min;
            return liMinBarber.get(VIP);
        }
    }
}
